package game.block.tile;

import game.block.Constants;
import game.block.blocks.JumpBlock;
import game.block.blocks.StoneBlock;
import game.block.entity.Entity;
import game.block.entity.MovableEntity;
import game.block.entity.TrapEntity;
import game.block.entity.Wall;
import game.block.trap.Fire;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

public class Tile implements Constants {
    private Sprite sprite, fireSprite, movingToSprite, explodeBlockSprite, blankSprite, hoverSprite;
    private Vector2 position;
    private ArrayList<Entity> entities;
    private boolean spriteChanged;
    private COLOR color;

    public Tile(Vector2 pos) {
        position = pos;
        entities = new ArrayList<Entity>();
        spriteChanged = false;
        fireSprite = new Sprite(new Texture(Gdx.files.internal(FIRE_TILE)));
        hoverSprite = new Sprite(new Texture(Gdx.files.internal(HOVER_TILE)));
        movingToSprite = new Sprite(new Texture(Gdx.files.internal(MOVINGTO_TILE)));
        explodeBlockSprite = new Sprite(new Texture(Gdx.files.internal(EXPLODE_TILE)));
        blankSprite = new Sprite(new Texture(Gdx.files.internal(NORMAL_TILE)));
        sprite = blankSprite;
    }

    public void update(float delta) {

    }

    public void render(SpriteBatch sb) {
        if (spriteChanged) {
            changeSprite();
        }
        if(sprite!=null){
        sb.draw(sprite.getTexture(), position.x, position.y, 0, 0, sprite.getWidth(), sprite.getHeight(), SCALE, SCALE, 0, 0, 0, (int) sprite
                .getWidth(), (int) sprite.getHeight(), false, false);
        }
    }

    private void changeSprite() {
        switch (color) {
        case BLANK:
            if (containsFire()) {
                sprite = fireSprite;
            } else {
                sprite = blankSprite;
            }
            break;
        case HOVER:
            sprite = hoverSprite;
            break;
        case MOVINGTO:
            sprite = movingToSprite;
            break;
        case EXPLODE:
            sprite = explodeBlockSprite;
            break;

        }
        spriteChanged = false;
    }

    public boolean containsTrap() {
        for (Entity e : entities) {
            if (e instanceof TrapEntity) {
                return true;
            }
        }
        return false;
    }

    public boolean containsBlock() {
        for (Entity e : entities) {
            if (e instanceof MovableEntity) {
                return true;
            }
        }
        return false;
    }

    public boolean containsFire() {
        for (Entity e : entities) {
            if (e instanceof Fire) {
                return true;
            }
        }
        return false;
    }

    public void changeColor(COLOR c) {
        color = c;
        spriteChanged = true;
    }

    public Vector2 getPosition() {
        return position;
    }

    public void setPosition(Vector2 position) {
        this.position = position;
    }

    public boolean hasEntity() {
        return entities.size() != 0;
    }

    public ArrayList<Entity> getEntities() {
        return entities;
    }

    public void setEntities(ArrayList<Entity> entities) {
        this.entities = entities;
    }

    public boolean containsWall() {
        for (Entity e : entities) {
            if (e instanceof Wall) {
                return true;
            }
        }
        return false;
    }

    public void remove(int i) {
        entities.remove(i);
    }

    public void removeAll() {
        entities.clear();
    }

    public void add(Entity entity) {
        if (!entities.contains(entity)) {
            entities.add(entity);
        }
    }

    public void remove(Entity entity) {
        entities.remove(entity);
    }

    public boolean containsJumpBlock() {
        for (Entity e : entities) {
            if (e instanceof JumpBlock) {
                return true;
            }
        }
        return false;
    }

    public MovableEntity getBlock() {
        for (Entity e : entities) {
            if (e instanceof MovableEntity) {
                return (MovableEntity) e;
            }
        }
        return null;
    }

    public Sprite getSprite() {
        return sprite;
    }

    public void setSprite(Sprite sprite) {
        this.sprite = sprite;
    }

    public boolean containsStone() {
        for (Entity e : entities) {
            if (e instanceof StoneBlock) {
                return true;
            }
        }
        return false;
    }

}
